package com.free.framework.business;

import android.net.ParseException;

import com.free.framework.R;
import com.free.framework.lib.utils.LogHelper;
import com.free.framework.lib.utils.ResMgr;
import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.security.cert.CertificateException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * @author 李博
 * @date 16/9/10
 * @desc 统一错误处理
 */
public class ResponseExceptionHandler {

    private static final int SERVICE = 1;
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ResponseException handleResponseException(Throwable e) {
        LogHelper.e("ResponseExceptionHandler -> " + e.getCause().getMessage());
        ResponseException ex;

        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ResponseException(e, ERROR.HTTP_ERROR);
            switch (httpException.code()) {
                case UNAUTHORIZED:
                    ex = new ResponseException(e, ERROR.AUTH_ERRROR);
                    ex.message = ResMgr.getString(R.string.net_unauth_error);
                    break;
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    ex.message = ResMgr.getString(R.string.net_error);
                    break;
            }
            return ex;
        } else if (e instanceof JsonParseException || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new ResponseException(e, ERROR.PARSE_ERROR);
            ex.message = ResMgr.getString(R.string.net_parse_error);
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new ResponseException(e, ERROR.NETWORD_ERROR);
            ex.message = ResMgr.getString(R.string.net_connection_error);
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException
                || e instanceof CertificateException) {
            ex = new ResponseException(e, ERROR.SSL_ERROR);
            ex.message = ResMgr.getString(R.string.net_cert_validation_error);
            return ex;
        } else if (e instanceof java.net.SocketTimeoutException){
            ex = new ResponseException(e,ERROR.TIMEOUT_ERROR);
            ex.message = ResMgr.getString(R.string.net_time_out_error);
            return ex;
        }else {
            ex = new ResponseException(e, ERROR.UNKNOWN);
            ex.message = e.getMessage();
            return ex;
        }
    }

    /**
     * 约定异常
     */
    public static class ERROR {
        /**
         * 未知错误
         */
        public static final int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        public static final int PARSE_ERROR = 1001;
        /**
         * 网络错误
         */
        public static final int NETWORD_ERROR = 1002;
        /**
         * 协议出错
         */
        public static final int HTTP_ERROR = 1003;

        /**
         * 授权出错
         */
        public static final int AUTH_ERRROR = 1004;

        /**
         * 证书出错
         */
        public static final int SSL_ERROR = 1005;

        /**
         * 请求超时
         */
        public static final int TIMEOUT_ERROR = 1006;

    }


}
